package cn.edu.dgut.sai.mvc;

import lombok.Builder;
import lombok.Data;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author Sai
 * Created by Sai on 2019/9/8.
 *
 * @see WebMvcConfigurationSupport
 * @see ResourceHttpMessageConverter
 * @see GsonHttpMessageConverter
 * @see HttpMessageConverter
 *
 * HttpMessageConverter列表：
 * https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#rest-message-conversion
 */
@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig {

    @Controller
    static class testController {

        @RequestMapping(value = "/helloMVC", produces = "text/html;charset=utf-8")
        @ResponseBody
        public String testHello() {
            return "Hello Spring MVC 东莞理工学院!!!";
        }

        // CORS参考文档：
        // https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS
        // https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-cors
        //
        // 原因：浏览器禁止对当前源外的资源进行AJAX调用)解决方法：如果项目没有使用security,直接加@CrossOrigin注解在控制器类上面或控制器方法上即可。
        //
        // 测试命令： curl -v -H "Access-Control-Request-Method: GET" -H "Origin: http://localhost:9999" -X OPTIONS http://localhost:8080/spring-mvc-demo/app/testCORS
        @RequestMapping(value = "/testCORS", produces = "text/html;charset=utf-8")
        @ResponseBody
        @CrossOrigin
        public String testCORS() {
            return "测试CORS跨域请求";
        }

        @RequestMapping(value = "/testJson", produces = "application/json")
        @ResponseBody
        public User testJson() {
            return User.builder().name("Sai").number("2003066").build();
        }

        @RequestMapping("/testException")
        @ResponseBody
        public String testExption() {
            throw new RuntimeException("东莞理工学院");
        }

        // 官方参考文档：https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#rest-resttemplate
        @RequestMapping(value = "/testHttpClient", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
        @ResponseBody
        public ResponseEntity<Resource> testHttpClient() {

            RestTemplate rest = new RestTemplate();

            Resource resource = rest.getForObject("https://www.baidu.com/img/dong1_a1c52951c1f40e1496b46b9ae415c121.gif", Resource.class);

            return ResponseEntity.ok().contentType(MediaType.IMAGE_GIF).body(resource);
        }

        @ExceptionHandler
        @ResponseBody
        public ResponseEntity<String> exceptionHandler(Exception e) {
            return ResponseEntity
                    .ok()
                    .contentType(MediaType.APPLICATION_PROBLEM_JSON_UTF8)
                    .body("异常抛出了: " + e.getMessage());
        }

        @Data
        @Builder
        static class User {
            private String name;// 姓名
            private String number;// 工号
        }

    }
}

